/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.UserWeixin;
import org.springblade.modules.repair.service.IUserWeixinService;
import org.springblade.modules.repair.utils.WeiXinUtil;
import org.springblade.modules.repair.vo.weixin.CommonVO;
import org.springblade.modules.repair.vo.weixin.MessageTemplateVO;
import org.springblade.modules.repair.vo.weixin.MessageVO;
import org.springblade.modules.repair.vo.weixin.WxUserInfo;
import org.springblade.modules.system.entity.UserMiniappWeixin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户-微信信息 控制器
 *
 * @author zcc
 * @since 2021-05-26
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("repair/user/weixin")
@Api(value = "用户-微信信息", tags = "用户-微信信息接口")
public class UserWeixinController extends BladeController {

	private final IUserWeixinService userWeixinService;

	private final WeiXinUtil weiXinUtil;

	/**
	 * 新增 用户-微信信息
	 */
	@GetMapping("/save")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "新增", notes = "传入微信code")
	public R save(@RequestParam("code") String code) {
		String weiUserInfo = weiXinUtil.getWeiUserInfo(code);
		log.info("<---------------------------- {WECHAT_USER_INFO:}"+weiUserInfo+"------------------------------->");
		UserWeixin userWeixin = JSON.parseObject(weiUserInfo, UserWeixin.class);
		userWeixin.setUserId(AuthUtil.getUserId());
		UserWeixin oldUserWeixin = userWeixinService.getOne(Wrappers.<UserWeixin>lambdaQuery().eq(UserWeixin::getIsDeleted,0).eq(UserWeixin::getUserId,userWeixin.getUserId()));
		if(ObjectUtil.isNotEmpty(oldUserWeixin)){
			userWeixin.setId(oldUserWeixin.getId());
		}
		userWeixinService.saveOrUpdate(userWeixin);
		return R.status(Boolean.TRUE);
	}

	@GetMapping("/test")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "测试")
	public void test() {

		MessageTemplateVO mt = new MessageTemplateVO();
		mt.setTouser("o_9OJ5_-wuNsqMQPmO0vFqDpJYs4");
		mt.setTemplate_id("H0FdZCq--qf-GM40PKCibMlW7dHTzFsKaJJoSiOrJSk");
		//mt.setUrl("http://weixin.qq.com/download");
//		MiniprogramVO miniprogram = new MiniprogramVO();
//		miniprogram.setAppid("wxb8dc0f5451620604");
//		miniprogram.setPagepath("index?foo=bar");

		MessageVO mv = new MessageVO();
		CommonVO first = new CommonVO("DT1困人故障报修", "#173177");
		CommonVO keyword1 = new CommonVO("1号交易广场", "#173177");
		CommonVO keyword2 = new CommonVO("32104403002008008078", "#173177");
		CommonVO keyword3 = new CommonVO("DT1", "#173177");
		CommonVO keyword4 = new CommonVO("是", "#173177");
		CommonVO keyword5 = new CommonVO("2018年1月19日 20:00:01", "#173177");
		CommonVO remark = new CommonVO("请您协调处理", "#173177");
		mv.setFirst(first);
		mv.setKeyword1(keyword1);
		mv.setKeyword2(keyword2);
		mv.setKeyword3(keyword3);
		mv.setKeyword4(keyword4);
		mv.setKeyword5(keyword5);
		mv.setRemark(remark);
		mt.setData(mv);

		weiXinUtil.sendTemplateMessage(JSON.toJSONString(mt));
	}

	@GetMapping("/getAccessToken")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "获取AccessToken")
	public R getAccessToken() {
		return R.data(weiXinUtil.getAccessToken());
	}

	@GetMapping("/getUserInfo")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取userInfo")
	public R getUserInfo(String openid, String accessToken) {
		return R.data(weiXinUtil.getUserInfo(openid, accessToken));
	}


	@GetMapping("/official/account/info")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取公众号用户信息")
	public R<WxUserInfo> getBindWxOfficialAccountInfo(Long userId) {
		UserWeixin userWeixin = userWeixinService.getOne(Wrappers.<UserWeixin>lambdaQuery().eq(UserWeixin :: getUserId, userId).eq(UserWeixin :: getIsDeleted, 0), false);
		if (userWeixin == null) {
			return R.data(new WxUserInfo());
		}

//		String accessToken = weiXinUtil.getAccessToken();
//		WxUserInfo wxUserInfo = weiXinUtil.getUserInfo(userWeixin.getOpenid(), accessToken);
		WxUserInfo wxUserInfo = new WxUserInfo();
		wxUserInfo.setSubscribe(userWeixin.getSubscribe());
		return R.data(wxUserInfo);
	}

}
